﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using System.Text;

namespace BaoJianSoft.Common.Web
{
	/// <summary>
	/// Restful error message that can be displayed on the browser.
	/// </summary>
	public class RestfulErrorMessage : Message
	{
		private MessageHeaders messageHeaders;
		private MessageProperties messageProperties;
		private MessageVersion messageVersion;
		private HttpStatusCode statusCode;
		private string statusDescription;

		#region Constructors

		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="statusCode">Http response status code.</param>
		/// <param name="statusDescription">Http response status description.</param>
		public RestfulErrorMessage(HttpStatusCode statusCode, string statusDescription)
		{
			messageVersion = MessageVersion.None;
			messageHeaders = new MessageHeaders(messageVersion);
			messageProperties = new MessageProperties();

            this.statusCode = statusCode;
			this.statusDescription = statusDescription;

			HttpResponseMessageProperty prop = new HttpResponseMessageProperty();
			prop.StatusCode = statusCode;
			prop.StatusDescription = statusDescription;
			messageProperties[HttpResponseMessageProperty.Name] = prop;

			if (WebOperationContext.Current != null)
			{
				WebOperationContext.Current.OutgoingResponse.StatusCode = statusCode;
				WebOperationContext.Current.OutgoingResponse.StatusDescription = statusDescription;
				WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = false;
			}
		}

		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="ex">Restful exception.</param>
		public RestfulErrorMessage(RestfulException ex)
			: this(ex.StatusCode, ex.StatusDescription)
		{

		}

		#endregion

		#region Virtual Properties

		/// <summary>
		/// Gets the headers of the message.
		/// </summary>
		public override MessageHeaders Headers
		{
            get { return this.messageHeaders; }
		}



		/// <summary>
		/// Gets the properties of the message.
		/// </summary>
		public override MessageProperties Properties
		{
            get { return this.messageProperties; }
		}

		/// <summary>
		/// Gets the version of the message.
		/// </summary>
		public override MessageVersion Version
		{
            get { return this.messageVersion; }
		}

		#endregion

		/// <summary>
		/// Called when the entire message is written to an XML file.
		/// </summary>
		/// <param name="writer">A System.Xml.XmlDictionaryWriter that is used to write this message to an XML file.</param>
		protected override void OnWriteBodyContents(System.Xml.XmlDictionaryWriter writer)
		{
			writer.WriteStartElement("Error", ServiceNamespaces.Common);
			writer.WriteElementString("StatusCode", this.statusCode.ToString("d"));
			writer.WriteElementString("StatusDescription", this.statusDescription);
			writer.WriteEndElement();
		}
	}
}